package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.MD5;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author lhh
 * @ClassName AdminController
 * @date 2022/9/14 15:46
 */
@Controller
@RequestMapping("admin")
public class AdminController extends BaseController {

    @Reference
    private AdminService adminService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Reference
    private RoleService roleService;

    @RequestMapping
    public String getAll(ModelMap model, HttpServletRequest request){
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "admin/index";
    }

    @GetMapping("/create")
    public String create(ModelMap model) {
        return "admin/create";
    }

    @PostMapping("save")
    public String save(Admin admin){
        //密码加密处理
        //admin.setPassword(MD5.encrypt(admin.getPassword()));
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        //默认头像
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        adminService.save(admin);
        return "common/successPage";
    }

    @GetMapping("/edit/{id}")
    public String getById(@PathVariable Long id,Model model){
        Admin admin=adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    @PostMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return "common/successPage";
    }

    @GetMapping("/uploadShow/{id}")
    public String uploadShow(ModelMap model,@PathVariable Long id) {
        model.addAttribute("id", id);
        return "admin/upload";
    }

    @PostMapping("upload/{id}")
    public String upload(@PathVariable Long id,
                         @RequestParam(value = "file") MultipartFile filters,
                         HttpServletRequest request) throws IOException {
        String newFileName= UUID.randomUUID().toString();

        QiniuUtil.upload2Qiniu(filters.getBytes(),newFileName);
        String url="http://rigc48hkx.hb-bkt.clouddn.com/"+newFileName;

        Admin admin = adminService.getById(id);
        admin.setHeadUrl(url);
        admin.setId(id);
        adminService.update(admin);
        return "common/successPage";

    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        adminService.remove(id);
        return "redirect:/admin";
    }

    @GetMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId,Model model){
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        model.addAllAttributes(roleMap);
        model.addAttribute("adminId",adminId);
        return "admin/assignShow";
    }

    @PostMapping("assignRole")
    public String assignRole(Long adminId, Long[] roleIds){
        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }
}
